// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   newRegister_ok.java

package manufacture.apply;

import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class newRegister_ok extends HttpServlet
{

	public newRegister_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		counter count = new counter(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		try
		{
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String product_amount = request.getParameter("product_amount");
				int num = Integer.parseInt(product_amount);
				int p = 0;
				for (int i = 1; i < num; i++)
				{
					String tem_amount = (new StringBuilder("amount")).append(i).toString();
					String amount = request.getParameter(tem_amount);
					if (!validata.validata(amount))
						p++;
				}

				if (p == 0)
				{
					String time = "";
					Date now = new Date();
					SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
					time = formatter.format(now);
					String reason = request.getParameter("reason");
					String register = request.getParameter("register");
					String register_time = request.getParameter("register_time");
					String demand_gather_time = request.getParameter("demand_gather_time");
					String bodyb = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
					String remark = exchange.toHtml(bodyb);
					try
					{
						String apply_ID = NseerId.getId(getClass().getResource("").toString(), (String)dbSession.getAttribute("unit_db_name"));
						double real_cost_price_sum = 0.0D;
						double demand_amount = 0.0D;
						for (int i = 1; i < num; i++)
						{
							String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
							String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
							String tem_product_describe = (new StringBuilder("product_describe")).append(i).toString();
							String tem_amount = (new StringBuilder("amount")).append(i).toString();
							String tem_real_cost_price = (new StringBuilder("real_cost_price")).append(i).toString();
							String tem_type = (new StringBuilder("type")).append(i).toString();
							String product_name = request.getParameter(tem_product_name);
							String product_ID = request.getParameter(tem_product_ID);
							String product_describe = request.getParameter(tem_product_describe);
							String amount = request.getParameter(tem_amount);
							String real_cost_price2 = request.getParameter(tem_real_cost_price);
							String type = request.getParameter(tem_type);
							StringTokenizer tokenTO3 = new StringTokenizer(real_cost_price2, ",");
							String real_cost_price;
							String real_cost_price1;
							for (real_cost_price = ""; tokenTO3.hasMoreTokens(); real_cost_price = (new StringBuilder(String.valueOf(real_cost_price))).append(real_cost_price1).toString())
								real_cost_price1 = tokenTO3.nextToken();

							if (!amount.equals("") && Double.parseDouble(amount) != 0.0D)
							{
								double real_cost_price_subtotal = Double.parseDouble(real_cost_price) * Double.parseDouble(amount);
								real_cost_price_sum += real_cost_price_subtotal;
								demand_amount += Double.parseDouble(amount);
								String sql = (new StringBuilder("insert into manufacture_apply(apply_ID,product_ID,product_name,product_describe,amount,remark,register,register_time,type,pay_id_group) values('")).append(apply_ID).append("','").append(product_ID).append("','").append(product_name).append("','").append(product_describe).append("','").append(amount).append("','").append(remark).append("','").append(register).append("','").append(register_time).append("','").append(type).append("','").append(reason).append("')").toString();
								manufacture_db.executeUpdate(sql);
							}
						}

						List rsList = GetWorkflow.getList(manufacture_db, "manufacture_config_workflow", "03");
						String elem[] = new String[3];
						if (rsList.size() == 0)
						{
							String pay_ID_group = "";
							String sql = (new StringBuilder("update manufacture_apply set check_tag='1' where apply_ID='")).append(apply_ID).append("'").toString();
							manufacture_db.executeUpdate(sql);
							sql = (new StringBuilder("select * from manufacture_apply where apply_ID='")).append(apply_ID).append("'").toString();
							ResultSet rs1 = manufacture_db.executeQuery(sql);
							if (rs1.next())
								pay_ID_group = rs1.getString("pay_ID_group");
							for (StringTokenizer tokenTO = new StringTokenizer(pay_ID_group, ", "); tokenTO.hasMoreTokens();)
							{
								sql = (new StringBuilder("select * from stock_pay where pay_ID='")).append(tokenTO.nextToken()).append("'").toString();
								rs1 = manufacture_db.executeQuery(sql);
								if (rs1.next())
								{
									sql = (new StringBuilder("update crm_order set manufacture_tag='2' where order_ID='")).append(rs1.getString("reasonexact")).append("'").toString();
									manufacture_db.executeUpdate(sql);
								}
							}

						} else
						{
							String sql;
							for (Iterator ite = rsList.iterator(); ite.hasNext(); manufacture_db.executeUpdate(sql))
							{
								elem = (String[])ite.next();
								sql = (new StringBuilder("insert into manufacture_workflow(config_id,object_ID,describe1,describe2,type_id) values ('")).append(elem[0]).append("','").append(apply_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("','03')").toString();
							}

						}
					}
					catch (Exception ex)
					{
						ex.printStackTrace();
					}
					response.sendRedirect("manufacture/apply/newRegister_ok_a.jsp");
				} else
				{
					response.sendRedirect("manufacture/apply/newRegister_ok_b.jsp");
				}
				manufacture_db.commit();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
